package com.map.components.https;

import org.apache.http.Header;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class ResponseEntity {
    private String result;
    private Integer status;
    private Header[] allHeaders;
    private String setCookie;
    private Document document;

    public String getHeader(String header) {
        for(int i=0; i<allHeaders.length; i++) {
            if(header.equals(allHeaders[i].getName())) {
                return allHeaders[i].getValue();
            }
        }
        return "";
    }

    public Elements selectDoc(String cssQuery) {
        if(null == this.document) {
            this.document = Jsoup.parse(this.result);
        }
        return  this.document.select(cssQuery);
    }

    public Element selectDocOnlyOne(String cssQuery) {
        if(null == this.document) {
            this.document = Jsoup.parse(this.result);
        }
        Elements elements = this.document.select(cssQuery);
        if(null != elements && elements.size() > 0) {
            return elements.get(0);
        }
        return null;
    }


    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Header[] getAllHeaders() {
        return allHeaders;
    }

    public void setAllHeaders(Header[] allHeaders) {
        this.allHeaders = allHeaders;
    }


    public String getSetCookie() {
        return setCookie;
    }

    public void setSetCookie(String setCookie) {
        this.setCookie = setCookie;
    }

    public Document getDocument() {
        if(null == this.document) {
            this.document = Jsoup.parse(this.result);
        }
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }
}
